package com.example.winterholidayfile.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.winterholidayfile.entity.Hanjiazuoye;
import com.example.winterholidayfile.entity.Result;
import com.example.winterholidayfile.service.IHanjiazuoyeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fjs
 * @since 2023-01-18
 */
@RestController
@CrossOrigin
@RequestMapping("/hanjiazuoye")
public class HanjiazuoyeController {

    @Resource
    private IHanjiazuoyeService iHanjiazuoyeService;
    /*
    登录
     */
    @PostMapping("loginuser")
    public Result Userlogin(Hanjiazuoye hanjiazuoye){
        System.out.println("hanjiazuoye ========================================== " + hanjiazuoye);
        if (hanjiazuoye.getName()!=""&&hanjiazuoye.getPassword()!=""){
            if (hanjiazuoye.getName()!="null"&&hanjiazuoye.getPassword()!="null"){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.like("name", hanjiazuoye.getName());
                queryWrapper.like("password", hanjiazuoye.getPassword());
                List list = iHanjiazuoyeService.list(queryWrapper);
                if(list.toArray().length==0){
                    Result result = new Result();
                    result.setCode(500);
                    return result;
                }else {
                    Result result = new Result();
                    result.setCode(201);
                    result.setT(list);
                    return  result;
                }
            }else {
                Result result = new Result();
                result.setCode(500);
                return result;
            }

        }else {
            Result result = new Result();
            result.setCode(500);
            return result;
        }

    }



    @GetMapping("All")
    public Result All(Integer current,Integer size,Hanjiazuoye hanjiazuoye){
        System.out.println("hanjiazuoye ================================================= " + hanjiazuoye);
        QueryWrapper queryWrapper = new QueryWrapper();
        // 匹配查找到id=1的数据
        if (StringUtils.isNotBlank(hanjiazuoye.getName())){
            queryWrapper.like("name",hanjiazuoye.getName());
        }

        Page page = new Page(current, size);
        Page page1 = iHanjiazuoyeService.page(page,queryWrapper);

        return new Result(page1);
    }

    /**
     * 添加  修改
     * @param hanjiazuoye
     * @return
     */
    @PostMapping("adduser")
    @ResponseBody
    public Result addUser(Hanjiazuoye hanjiazuoye){
        Result result = new Result();
        if (hanjiazuoye.getId()==null){
            // 这里是添加操作
            QueryWrapper<Hanjiazuoye> queryWrapper = new QueryWrapper<>(hanjiazuoye);

            QueryWrapper<Hanjiazuoye> Hanjiazuoye1 = queryWrapper.like("name", hanjiazuoye.getName());
            List<Hanjiazuoye> list1 = iHanjiazuoyeService.list(Hanjiazuoye1);
            // 是null 就添加   是空也添加
            for (Hanjiazuoye hanjiazuoye1 : list1) {
                if (hanjiazuoye1.getName()==null){
                    return new Result(iHanjiazuoyeService.saveOrUpdate(hanjiazuoye));
                }else {
                    result.setCode(500);
                    return result;
                }
            }

        }else {
            // 这里是修改操作
            return new Result(iHanjiazuoyeService.saveOrUpdate(hanjiazuoye));
        }
        result.setCode(400);
        return result;
    }

    @DeleteMapping("deluser{id}")
    public Result deluser(@PathVariable Integer id){
        // 匹配查找到id=1的数据
        return new Result(iHanjiazuoyeService.removeById(id));
    }


    @PostMapping("verifyName")
    @ResponseBody
    public Result verifyName(Hanjiazuoye hanjiazuoye){
        System.out.println("hanjiazuoye = " + hanjiazuoye);
        QueryWrapper<Hanjiazuoye> queryWrapper = new QueryWrapper<>(hanjiazuoye);
        // 匹配查找到id=1的数据
        queryWrapper.like("name",hanjiazuoye.getName());
        return new Result(iHanjiazuoyeService.list(queryWrapper));

    }

}

